package edu.scu.declaresys.web.action;

import java.util.Date;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.IProjectService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.vo.Area;
import edu.scu.declaresys.vo.Project;
import edu.scu.declaresys.vo.User;
import edu.scu.declaresys.web.ABaseAction;
import edu.scu.declaresys.web.util.SessionUtil;

/**
 * 项目推荐Action
 * @author: zt
 * @since : 2013-7-9
 */
public class RcmdProjectAction extends ABaseAction implements
		ModelDriven<Project>
{
	private static final long	serialVersionUID	= -4677401059746028165L;

	private IProjectService projectService = ServiceFactory.createProjectService();
	
	private Project project = new Project();
	
	private Integer page;
	
	//
	// 查找项目
	//
	public String rcmdWaitRcmdProject() throws Exception
	{
		User crtUser = SessionUtil.getCurrentUser(session);
		if(crtUser == null || crtUser.getId() == null
				|| crtUser.getLevel() == null
				|| crtUser.isRecommender() == false)
		{
			throw new Exception("用户不合法或未登陆");
		}
		PageList<Project> projectLst = projectService.findWaitRcmdProjectList(crtUser.getId(),
				crtUser.getLevel(), project.getProjectClassId(),
				project.getYear(), project.getName(), page);
		request.setAttribute(IAttrName.PROJECT_LIST, projectLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, projectLst.size());
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	public String rcmdMyRcmdedProject() throws Exception
	{
		User crtUser = SessionUtil.getCurrentUser(session);
		if(crtUser == null || crtUser.getId() == null
				|| crtUser.getLevel() == null
				|| crtUser.isRecommender() == false)
		{
			throw new Exception("用户不合法或未登陆");
		}
		PageList<Project> projectLst = projectService.findRcmdedProjectList(crtUser.getId(), crtUser.getLevel(),
				project.getProjectClassId(), project.getYear(), project.getState(), project.getName(), page);
		request.setAttribute(IAttrName.PROJECT_LIST, projectLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, projectLst.size());
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		//
		return Action.SUCCESS;
	}

	//
	// 推荐或不推荐项目
	//
	public String rcmdProject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定要推荐的项目");
		}
		/**
		 * check limits.
		 */
		User user = SessionUtil.getCurrentUser(session);
		if(!user.isRecommender())
		{
			throw new Exception("当前用户非项目推荐人员");
		}
		// Check project state. and set next state. and recommend time.
		Project crtProject = projectService.findById(project.getId(), Depth.ZERO);
		if(crtProject == null)
		{
			throw new Exception("指定的项目不存在");
		}
		int level = user.getLevel();
		int state = crtProject.getState();
		int nextState = -1;
		if(level == Area.LEVEL_COUNTY)
		{
			if(state != Project.STATE_UNRCMD_COUNTY)
			{
				throw new Exception("项目已经被推荐");
			}
			nextState = Project.STATE_UNRCMD_CITY;
			project.setCountyRecTime(new Date());
		}
		else if(level == Area.LEVEL_CITY)
		{
			if(state != Project.STATE_COUNTY_REQUEST_CANCEL_RCMD
					&& state != Project.STATE_UNRCMD_CITY)
			{
				throw new Exception("项目已经被推荐");
			}
			nextState = Project.STATE_UNRCMD_PROVINCE;
			project.setCityRecTime(new Date());
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			if(state != Project.STATE_CITY_REQUEST_CANCEL_RCMD
					&& state != Project.STATE_UNRCMD_PROVINCE)
			{
				throw new Exception("项目已经被推荐");
			}
			nextState = Project.STATE_UNAUDIT_UNMODIFIABLE;
			project.setProvinceRecTime(new Date());
		}
		else
		{
			throw new Exception("用户不合法");
		}
		/**
		 * 更新项目状态
		 */
		Project newProject = new Project();
		newProject.setId(project.getId());
		newProject.setState(nextState);
		projectService.update(newProject);
		//
		return Action.SUCCESS;
	}
	
	public String rcmdDisrcmdProject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定要推荐的项目");
		}
		/**
		 * check limits.
		 */
		User user = SessionUtil.getCurrentUser(session);
		if(!user.isRecommender())
		{
			throw new Exception("当前用户非项目推荐人员");
		}
		/**
		 *  Check project state.
		 */
		Project crtProject = projectService.findById(project.getId(), Depth.ZERO);
		int level = user.getLevel();
		int state = crtProject.getState();
		if(level == Area.LEVEL_COUNTY)
		{
			if(state != Project.STATE_UNRCMD_COUNTY)
			{
				throw new Exception("项目已经被推荐");
			}
			project.setCountyRecTime(new Date());
		}
		else if(level == Area.LEVEL_CITY)
		{
			if(state != Project.STATE_COUNTY_REQUEST_CANCEL_RCMD
					&& state != Project.STATE_UNRCMD_CITY)
			{
				throw new Exception("项目已经被推荐");
			}
			project.setCityRecTime(new Date());
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			if(state != Project.STATE_CITY_REQUEST_CANCEL_RCMD
					&& state != Project.STATE_UNRCMD_PROVINCE)
			{
				throw new Exception("项目已经被推荐");
			}
			project.setProvinceRecTime(new Date());
		}
		else
		{
			throw new Exception("用户不合法");
		}
		/**
		 * 更新项目状态
		 */
		Project newProject = new Project();
		newProject.setState(Project.STATE_REJECTED);
		newProject.setId(project.getId());
		projectService.update(newProject);
		//
		return Action.SUCCESS;
	}
	
	//
	// 取消推荐请求相关
	//
	public String rcmdRequestCancelRcmd() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定要推荐的项目");
		}
		/**
		 * check limits.
		 */
		User user = SessionUtil.getCurrentUser(session);
		if(!user.isRecommender())
		{
			throw new Exception("当前用户非项目推荐人员");
		}
		/**
		 *  Check project state.
		 */
		Project crtProject = projectService.findById(project.getId(), Depth.ZERO);
		int level = user.getLevel();
		int state = crtProject.getState();
		int nextState = -1;
		if(level == Area.LEVEL_COUNTY)
		{
			if(state != Project.STATE_UNRCMD_CITY)
			{
				throw new Exception("项目已经不可以取消推荐，或已经发出取消推荐请求");
			}
			nextState = Project.STATE_COUNTY_REQUEST_CANCEL_RCMD;
		}
		else if(level == Area.LEVEL_CITY)
		{
			if(state != Project.STATE_UNRCMD_PROVINCE)
			{
				throw new Exception("项目已经不可以取消推荐，或已经发出取消推荐请求");
			}
			nextState = Project.STATE_CITY_REQUEST_CANCEL_RCMD;
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			throw new Exception("省级推荐人员不可以发出取消推荐请求");
		}
		else
		{
			throw new Exception("用户不合法");
		}
		/**
		 * 更新项目状态
		 */
		Project newProject = new Project();
		newProject.setState(nextState);
		newProject.setId(project.getId());
		projectService.update(newProject);
		//
		return Action.SUCCESS;
	}

	/**
	 * 查找当前推荐人员需要处理的下级请求取消推荐的项目
	 *@return
	 *@throws Exception
	 */
	public String rcmdDealCancelRcmdList() throws Exception
	{
		User crtUser = SessionUtil.getCurrentUser(session);
		if(crtUser == null || crtUser.getId() == null
				|| crtUser.getLevel() == null
				|| crtUser.isRecommender() == false)
		{
			throw new Exception("用户不合法或未登陆");
		}
		if(crtUser.getLevel() == Area.LEVEL_COUNTY)
		{
			throw new Exception("非法操作，县级推荐人员不能使用该功能");
		}
		PageList<Project> projectLst = projectService.findCancelRcmdProject(crtUser.getId(), crtUser.getLevel(),
				project.getProjectClassId(), project.getYear(), project.getName(), page);
		request.setAttribute(IAttrName.PROJECT_LIST, projectLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, projectLst.size());
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		//
		return Action.SUCCESS;
	}
	
	public String rcmdDealCancelRcmdConfirm() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定要推荐的项目");
		}
		/**
		 * check limits.
		 */
		User user = SessionUtil.getCurrentUser(session);
		if(!user.isRecommender())
		{
			throw new Exception("当前用户非项目推荐人员");
		}
		/**
		 *  Check project state.
		 */
		Project crtProject = projectService.findById(project.getId(), Depth.ZERO);
		int level = user.getLevel();
		int state = crtProject.getState();
		if(level == Area.LEVEL_COUNTY)
		{
			throw new Exception("县级用户不具有处理取消推荐请求的权限");
		}
		else if(level == Area.LEVEL_CITY)
		{
			if(state != Project.STATE_COUNTY_REQUEST_CANCEL_RCMD)
			{
				throw new Exception("下级推荐人员并未发出取消推荐请求");
			}
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			if(state != Project.STATE_CITY_REQUEST_CANCEL_RCMD)
			{
				throw new Exception("下级推荐人员并未发出取消推荐请求");
			}
		}
		else
		{
			throw new Exception("用户不合法");
		}
		/**
		 * 更新项目状态
		 */
		Project newProject = new Project();
		newProject.setState(Project.STATE_REJECTED);
		newProject.setId(project.getId());
		projectService.update(newProject);
		//
		return Action.SUCCESS;
	}
	
	public String rcmdDealCancelRcmdReject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定要推荐的项目");
		}
		/**
		 * check limits.
		 */
		User user = SessionUtil.getCurrentUser(session);
		if(!user.isRecommender())
		{
			throw new Exception("当前用户非项目推荐人员");
		}
		/**
		 *  Check project state.
		 */
		Project crtProject = projectService.findById(project.getId(), Depth.ZERO);
		int level = user.getLevel();
		int state = crtProject.getState();
		int nextState = -1;
		if(level == Area.LEVEL_COUNTY)
		{
			throw new Exception("县级用户不具有处理取消推荐请求的权限");
		}
		else if(level == Area.LEVEL_CITY)
		{
			if(state != Project.STATE_COUNTY_REQUEST_CANCEL_RCMD)
			{
				throw new Exception("下级推荐人员并未发出取消推荐请求");
			}
			nextState = Project.STATE_UNRCMD_CITY;
		}
		else if(level == Area.LEVEL_PROVINCE)
		{
			if(state != Project.STATE_CITY_REQUEST_CANCEL_RCMD)
			{
				throw new Exception("下级推荐人员并未发出取消推荐请求");
			}
			nextState = Project.STATE_UNRCMD_PROVINCE;
		}
		else
		{
			throw new Exception("用户不合法");
		}
		/**
		 * 更新项目状态
		 */
		Project newProject = new Project();
		newProject.setState(nextState);
		newProject.setId(project.getId());
		projectService.update(newProject);
		//
		return Action.SUCCESS;
	}
	
	//
	// Getts and Setters
	//
	@Override
	public Project getModel()
	{
		return project;
	}

	public Project getProject()
	{
		return project;
	}

	public void setProject(Project project)
	{
		this.project = project;
	}

	public Integer getPage()
	{
		return page;
	}

	public void setPage(Integer page)
	{
		this.page = page;
	}
	
	
}
